#include "FreeRTOS.h"
#include "task.h"
#include "stdio.h"

#include "led.h"
#include "beep.h"
#include "usart.h"
#include "adc.h"
#include "oled.h"
#include "bmp.h"
#include "key.h"
#include "time.h"
#include "my_delay.h"
#include "dht11.h"
#include "exti.h"
#include "fan.h"

#include "rc522_config.h"
#include "rc522_function.h"
#include <stdbool.h>




char window_num = 1;	//OLED屏页面
char commend_buf2[128] = {0};	//远程消息缓冲区 
char commend_flag = 0;	//收到的消息状态，0已读，1未读

uint8_t ucStatusReturn;    //返回RFID状态
uint8_t ucArray_ID [ 4 ];	 //先后存放IC卡的类型和UID(IC卡序列号)																					  

TaskHandle_t oled_handle;	//OLED屏任务
TaskHandle_t sensor_handle;	//传感器数据上传任务
TaskHandle_t dht11__handle;	//温湿度传感器获取任务
TaskHandle_t rc522_handle;	//RFID射频识别任务

//传感器数据
typedef struct SensorData
{
	char light;
	char temperature;
	char humidity;	
}SensorData;


//设备运行状态
typedef struct DeviceStatu
{
	char led1_statu[5];
	char led2_statu[5];
	char led3_statu[5];
	char beep_statu[5];
	char fan_statu[5];
	char fan_level[5];
}DeviceStatu;

SensorData sensor_data;
DeviceStatu device_statu;

void oledWindow1();
void oledWindow2();
void oledWindow3();

//OLED屏页面一
void oledWindow1()
{
	window_num = 1;
	temperature = 0;
	OLED_ShowPicture(5,20,128,35,BMP3,1);
	OLED_Refresh();
	while(window_num  == 1 && commend_flag == 0 && ucStatusReturn != MI_OK)
	{		
	}
}

//OLED屏页面二
void oledWindow2()
{
	window_num = 2;;
	OLED_ShowChinese(12,2,0,16,1);//华
	OLED_ShowChinese(30,2,1,16,1);//清
	OLED_ShowChinese(48,2,2,16,1);//远
	OLED_ShowChinese(66,2,3,16,1);//见
	OLED_ShowChinese(84,2,4,16,1);//教
	OLED_ShowChinese(102,2,5,16,1);//育
	//LED1:X(开/关)
	OLED_ShowString(8,20,"LED1:",12,1);
	//LED2:X(开/关)
	OLED_ShowString(70,20,"LED2:",12,1);
	//LED3:X(开/关)
	OLED_ShowString(8,35,"LED3:",12,1);
	//蜂鸣器:X(开/关)
	OLED_ShowChinese(70,35,2,12,1);
	OLED_ShowChinese(82,35,3,12,1);
	OLED_ShowChinese(94,35,4,12,1);
	OLED_ShowString(106,35,":",12,1);
	//光照强度:xxx(三位数数字)
	OLED_ShowChinese(8,50,5,12,1);
	OLED_ShowChinese(20,50,6,12,1);
	OLED_ShowChinese(32,50,7,12,1);
	OLED_ShowChinese(44,50,8,12,1);
	OLED_ShowString(56,50,":",12,1);
	while(window_num == 2 && commend_flag == 0 &&ucStatusReturn != MI_OK)
	{
		//LED1
		if(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_8) == 1)
		{
			OLED_ShowChinese(40,20,1,12,1);
		}
		else
		{
			OLED_ShowChinese(40,20,0,12,1);
		}
		//LED2
		if(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_9) == 1)
		{
			OLED_ShowChinese(102,20,1,12,1);
		}
		else
		{
			OLED_ShowChinese(102,20,0,12,1);
		}
		//LED3
		if(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_10) == 1)
		{
			OLED_ShowChinese(40,35,1,12,1);
		}
		else
		{
			OLED_ShowChinese(40,35,0,12,1);
		}
		//蜂鸣器
		if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_10) == 1)
		{
			OLED_ShowChinese(113,35,0,12,1);
		}
		else
		{
			OLED_ShowChinese(113,35,1,12,1);
		}
		//光照强度--动态
		OLED_ShowNum(64,50,light,3,12,1);
		OLED_Refresh();
	}
}

//OLED屏页面三
void oledWindow3()
{
	window_num = 3;
	OLED_ShowChinese(12,2,0,16,1);//华
	OLED_ShowChinese(30,2,1,16,1);//清
	OLED_ShowChinese(48,2,2,16,1);//远
	OLED_ShowChinese(66,2,3,16,1);//见
	OLED_ShowChinese(84,2,4,16,1);//教
	OLED_ShowChinese(102,2,5,16,1);//育
	//---温度
	OLED_ShowChinese(8,35,9,12,1);
	OLED_ShowChinese(20,35,8,12,1);
	OLED_ShowString(32,35,":",12,1);
	//---湿度
	OLED_ShowChinese(70,35,10,12,1);
	OLED_ShowChinese(82,35,8,12,1);
	OLED_ShowString(94,35,":",12,1);
	//温湿度数据--动态
	while(window_num == 3 && commend_flag == 0 && ucStatusReturn != MI_OK)
	{
		OLED_ShowNum(39,35,temperature,3,12,1);	//显示温度
		OLED_ShowNum(101,35,humidity,3,12,1);	//显示湿度
		OLED_Refresh();
	}
}

//显示屏任务
void displayOled(void *pvParameters)
{
	while(1)
	{
		OLED_Clear();
		switch (window_num)
		{
		case 1:
			oledWindow1();
			break;
		case 2:
			oledWindow2();
			break;
		case 3:
			oledWindow3();
			break;
		}
		if(commend_flag == 1)
		{
			commend_flag = 0;
			OLED_Clear();
			OLED_ShowChinese(30,2,6,16,1);//收
			OLED_ShowChinese(48,2,7,16,1);//到
			OLED_ShowChinese(66,2,8,16,1);//消
			OLED_ShowChinese(84,2,9,16,1);//息
			OLED_ShowString(0,30,commend_buf2,12,1);
			OLED_Refresh();
			vTaskDelay(2000);
		}
		if(ucStatusReturn == MI_OK)
		{
			OLED_Clear();
			OLED_ShowChinese(30,20,11,12,1);//门
			OLED_ShowChinese(48,20,12,12,1);//已
			OLED_ShowChinese(66,20,13,12,1);//开
			OLED_ShowChinese(84,20,14,12,1);//启
			OLED_Refresh();
			vTaskDelay(2000);
		}
	}
}

//读取传感器数据和设备状态
void readDht11Data(void *pvParameters)
{
	while(1)
	{
		sensor_data.light = 100-100*adcAverageValue()/4096;
		taskENTER_CRITICAL();
		dht11ReadData(&sensor_data.temperature, &sensor_data.humidity);//读取温湿度传感器时，必须保证时序，此时任务不可被切换
		taskEXIT_CRITICAL();
		//LED1运行状态
		if(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_8) == RESET)
		{
			strcpy(device_statu.led1_statu,"on");
		}
		else
		{
			strcpy(device_statu.led1_statu,"off");
		}
		//LED2运行状态
		if(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_9) == RESET)
		{
			strcpy(device_statu.led2_statu,"on");
		}
		else
		{
			strcpy(device_statu.led2_statu,"off");
		}
		//蜂鸣器运行状态
		if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_10) == SET)
		{
			strcpy(device_statu.beep_statu,"on");
		}
		else
		{
			strcpy(device_statu.beep_statu,"off");
		}
		//风扇运行状态
		if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15) == SET)
		{
			strcpy(device_statu.fan_statu,"on");
		}
		else
		{
			strcpy(device_statu.fan_statu,"off");
		}

		vTaskDelay(300);	
	}
}

//定时上传传感器数据
void sendSensorData(void *pvParameters)
{
	char ready_buf[30];
	while(1)
	{
		//发送传感器数据
		sprintf(ready_buf,"AT+CIPSEND=0,%d\r\n",sizeof(sensor_data);
		taskENTER_CRITICAL();
		usartSendStr(USART2,ready_buf,sizeof(ready_buf));//上传给上位机传感器数据
		taskEXIT_CRITICAL();
		
		taskENTER_CRITICAL();
		usartSendStr(USART2,(char*)(&sensor_data),sizeof(sensor_data));
		taskEXIT_CRITICAL();
		
		//发送设备运行状态
//		sprintf(ready_buf,"AT+CIPSEND=0,%d\r\n",sizeof(device_statu);
//		taskENTER_CRITICAL();
//		usartSendStr(USART2,ready_buf,sizeof(ready_buf));//上传给上位机传感器数据
//		taskEXIT_CRITICAL();
//		
//		taskENTER_CRITICAL();
//		usartSendStr(USART2,(char*)(&device_statu),sizeof(device_statu));
//		taskEXIT_CRITICAL();
		
		vTaskDelay(5000);
	}
}

//RFID射频识别
void rc522Test(void *pvParameters)
{
	while(1)
	{
		//RC522读取IC卡，进入范围则蜂鸣器提示
		taskENTER_CRITICAL();
		ucStatusReturn = PcdRequest(PICC_REQALL, ucArray_ID);
		taskEXIT_CRITICAL();
		if(ucStatusReturn == MI_OK  )
		{
			beepOpen();
			vTaskDelay(300);
			beepClose();
		}
		vTaskDelay(300);
	}
}

//主程序入口
int main(){
	
	//中断优先级分组
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);	
	tim7Init();//基本定时器7初始化--延时定时器
	tim6Init();//基本定时器6初始化--按键复用判断定时器
	tim2Init();//通用定时器2初始化--定时进入中断获取传感器数据获取

	//按键1，2，3，4初始化
	keyInit(GPIO_Pin_4);
	keyInit(GPIO_Pin_5);
	keyInit(GPIO_Pin_6);
	key4Init();
	
	//led1，2，3初始化
	ledInit(GPIO_Pin_8);
	ledInit(GPIO_Pin_9);
	ledInit(GPIO_Pin_10);
	beepInit();//蜂鸣器初始化
	fanInit();//风扇由N型MOS管控制，需要在N端提供高电平导通

	OLED_Init();//OLED屏幕初始化
	usart1Init();//usart1初始化，用于输出调试信息
	usart2Init();//usart2初始化，用于与ESP12S连接实现无线通信
	adcInit();//adc初始化，用于获取光照传感器数据
	dht11Init();//dht11温湿度传感器初始化
	
	tim1Init();//高级定时器1初始化--用于led3灯的pwm设置
	tim12Init();//通用定时器12初始化--用于fan风扇的pwm设置
	

	RC522_Init ();	//RFID-RC522射频识别初始化
	PcdReset ();	//RFID-RC522复位
	M500PcdConfigISOType ( 'A' );//设置RFID-RC522工作方式

	extiInit();//按键外部中断初始化
	wifiInit();//wifi初始化	
	TIM_Cmd(TIM2, ENABLE);

	xTaskCreate(displayOled,"displayOled",128,NULL,2,oled_handle);
	xTaskCreate(readDht11Data,"readDht11Data",128,NULL,2,dht11__handle);
	xTaskCreate(sendSensorData,"sendSensorData",128,NULL,2,sensor_handle);
	xTaskCreate(rc522Test,"rc522Test",128,NULL,2,rc522_handle);

	vTaskStartScheduler();
	


}






